package src.Server;

import src.User.User;

import java. io. IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net .*;
import java.util.HashMap;

import src.User.UserType;
import src.common.DBUtil;
import src.common.MessageType;
import src.common.impl.Message;

public class YYChatServer {
    public static HashMap hmSocket = new HashMap<String, Socket>();
    ServerSocket ss;

    Socket s;

    public YYChatServer() {
        try {
            ss = new ServerSocket(3456);
            System.out.println("服务器启动成功,正在监听3456端口 .. ");
            while (true){
                s = ss.accept();//等待客户端连接
                System.out.println("连接成功:" + s);
                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                User user = (User) ois.readObject();
                String username = user.getUsername();
                String password = user.getPassword();
                String phone = user.getPhone();
                System.out.println("服务端接收客户端登录信息username:"+username+" password:"+password);



                ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                Message message = new Message();

                if (user.getUserType().equals(UserType.USER_REGISTER)){
                    if (DBUtil.seekUser(username)){
                        message.setMessageType(MessageType.USER_REGISTER_FAILURE);
                    }else {
                        DBUtil.registerUser(username,password);
                        message.setMessageType(MessageType.USER_REGISTER_SUCCESS);
                    }

                    oos.writeObject(message);
                    s.close();
                }

                if (user.getUserType().equals(UserType.USER_LOGIN_VALIDATE)){
                    boolean success = DBUtil.loginValidate(username,password);

                    if (success){
                        System.out.println("密码验证通过~");
                        String allFriends = DBUtil.seekAllFriends(username,1);
                        message.setContent(allFriends);
                        message.setMessageType(MessageType.LOGIN_SUCCESS);
                        oos.writeObject(message);
                        hmSocket.put(username,s);
                        new ServerReceiverThread(s).start();
                        System.out.println("线程启动成功");
                    }else {
                        System.out.println("密码错误~~~");
                        message.setMessageType(MessageType.LOGIN_FAIL);
                        oos.writeObject(message);
                        s.close();
                    }
                }

                if (user.getUserType().equals(UserType.USER_LOGIN_BY_PHONE)){
                    boolean success = DBUtil.loginByPhone(phone,password);

                    if (success){
                        System.out.println("密码验证通过~");
                        String allFriends = DBUtil.seekAllFriends(username,1);
                        message.setContent(allFriends);
                        message.setMessageType(MessageType.LOGIN_SUCCESS);
                        oos.writeObject(message);
                        hmSocket.put(username,s);
                        new ServerReceiverThread(s).start();
                        System.out.println("线程启动成功");
                    }else {
                        System.out.println("密码错误~~~");
                        message.setMessageType(MessageType.LOGIN_FAIL);
                        oos.writeObject(message);
                        s.close();
                    }
                }
            }
        }
        catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }


    }
}